﻿$(document).ready(function () {
        
    $(function () {
        var today = new Date();
        $('.calendar').datepicker({
            inline: true,
            firstDay: 1,
            showOtherMonths: false,
            dayNamesMin: ['CN', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7'],
            monthNames: ["Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6", "Tháng 7", "Tháng 8", "Tháng 9", "Tháng 10", "Tháng 11", "Tháng 12"],
            dateFormat: 'yy-mm-dd'
        });
        //$("#calendarIn").datepicker("option", "minDate", "+0d");
    });


    $("#search").live("click", function () {
        var timeIn = $("#calendarIn").val();
        var hoTen = $("#hoTen").val();
        dataString = "timeIn=" + timeIn + "&hoTen=" + hoTen
        $.ajax({
            type: "GET",
            url: "Search_Reservation",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#results").html(data).hide().fadeIn();
                $(".hidden").hide();
                $(".hidden_new").hide();
                $(".showInfor").click(function () {
                    $("#info_" + $(this).attr("id")).toggle(200);
                })

                $(".showRoom").click(function () {
                    var str = $(this).attr("id").substr(8);
                    $("#showRoom_" + str).toggle(200);
                })

                $(".showCustomer").click(function () {
                    var str = $(this).attr("id").substr(5);
                    $("#showCustomer_" + str).toggle(200);
                })


                $(".showService").click(function () {
                    var str = $(this).attr("id").substr(5);
                    $("#showService_" + str).toggle(200);
                })


                $("#btn_add_room").click(function () {
                    $("#info_new").toggle("slow");
                })
            }
        });
    })


    $(".update_customer").live("click", function () {
        var maDS = $(this).attr("id").substr(7, 10);
        var CMND_old = $(this).attr("id").substr(17);
        var hoTen = $("#hoTen_" + maDS + CMND_old).val();
        var CMND = $("#cmnd_" + maDS + CMND_old).val();
        var dataString = "maDS=" + maDS + "&CMND=" + CMND + "&CMND_old=" + CMND_old + "&hoTen=" + hoTen
        $.ajax({
            type: "GET",
            url: "Update_Customer",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_customer_" + maDS).html(data).hide().fadeIn();
                setTimeout(function () {
                    $("#notice_customer_" + maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
            }
        });
    });


    $(".update_service").live("click", function () {
        var maDS = $(this).attr("id").substr(8,10);
        var maDV = $(this).attr("id").substr(18);
        var soLuong = $("#soLuong_" + maDS + maDV).val();
        var dataString = "maDS=" + maDS + "&maDV=" + maDV + "&soLuong=" + soLuong
        $.ajax({
            type: "GET",
            url: "Update_Service",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_service_" + maDS).html(data).hide().fadeIn();
                setTimeout(function () {
                    $("#notice_service_" + maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
            }
        });
    });

    $(".add_service").live("click", function () {
        var maDS = $(this).attr("id").substr(12);
        var maDV = $("#maDV_newDV_"+ maDS).val();
        var soLuong = $("#soLuong_newDV_"+ maDS).val();
        var dataString = "maDV=" + maDV + "&maDS=" + maDS + "&soLuong=" + soLuong
        $.ajax({
            type: "GET",
            url: "Add_Service",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_service_" + maDS).html("Thêm mới thành công").hide().fadeIn("slow");
                setTimeout(function () {
                    $("#notice_service_"+maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#ListService_" + maDS).append(data).hide().fadeIn("slow")
//                location.reload();
            }
        });
    });



    $(".delete_service").live("click", function () {
        var maDS = $(this).attr("id").substr(8,10);
        var maDV = $(this).attr("id").substr(18);
        var dataString = "maDS=" + maDS + "&maDV=" + maDV
        $.ajax({
            type: "GET",
            url: "Delete_Service",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_service_" + maDS).html(data).hide().fadeIn("slow");
                setTimeout(function () {
                    $("#notice_service_" + maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#"+maDS+maDV).fadeOut(300).remove()
            }
        });
    });


    $(".add_customer").live("click", function () {
        var maDS = $(this).attr("id").substr(8);
        var hoTen = $("#hoTen_newCus_" + maDS).val();
        var CMND = $("#cmnd_newCus_" + maDS).val();
        var dataString = "maDS=" + maDS + "&hoTen=" + hoTen + "&CMND=" + CMND
        $.ajax({
            type: "GET",
            url: "Add_Customer",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_customer_" + maDS).html("Thêm mới thành công").hide().fadeIn("slow");
                setTimeout(function () {
                    $("#notice_customer_" + maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#ListCustomer_" + maDS).append(data).hide().fadeIn("slow")
                //                location.reload();
            }
        });
    });



    $(".delete_customer").live("click", function () {
        var maDS = $(this).attr("id").substr(7,10);
        var CMND = $(this).attr("id").substr(17);
        var dataString = "maDS=" + maDS + "&CMND=" + CMND
        $.ajax({
            type: "GET",
            url: "Delete_Customer",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_customer_" + maDS).html(data).hide().fadeIn("slow");
                setTimeout(function () {
                    $("#notice_customer_" + maDS).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#" + maDS + CMND).fadeOut(300).remove()
            }
        });
    });


    $("#search_ct").live("click", function () {
        var timeIn = $("#calendarIn_ct").val();
        var hoTen = $("#hoTen_ct").val();
        dataString = "timeIn=" + timeIn + "&hoTen=" + hoTen
        $.ajax({
            type: "GET",
            url: "Search_Reservation_Company",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#results").html(data).hide().fadeIn();
                $(".hidden").hide();
                $(".hidden_new").hide();
                $(".showInfor").click(function () {
                    $("#info_" + $(this).attr("id")).toggle(200);
                })

                $(".showRoom").click(function () {
                    var str = $(this).attr("id").substr(8);
                    $("#showRoom_" + str).toggle(200);
                })

                $(".showCustomer").click(function () {
                    var str = $(this).attr("id").substr(5);
                    $("#showCustomer_" + str).toggle(200);
                })


                $(".showService").click(function () {
                    var str = $(this).attr("id").substr(5);
                    $("#showService_" + str).toggle(200);
                })


                $("#btn_add_room").click(function () {
                    $("#info_new").toggle("slow");
                })
            }
        });
    })


    $(".checkIn").live("click", function () {
        var maDP = $(this).attr("id").substr(1);
        var dataString = "maDP=" + maDP 
        $.ajax({
            type: "GET",
            url: "CheckIn",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_ds_" + maDP).html("Khách đã nhận phòng").hide().fadeIn();
                setTimeout(function () {
                    $("#notice_ds_" + maDP).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#checkIn_" + maDP).fadeOut(300).remove()
                $("#checkOut_" + maDP).fadeOut(300).fadeIn(300)
            }
        });
    });

    $(".checkOut").live("click", function () {
        var maDP = $(this).attr("id").substr(1);
        var dataString = "maDP=" + maDP
        $.ajax({
            type: "GET",
            url: "CheckOut",
            data: dataString,
            dataType: 'html',
            success: function (data) {
                $("#notice_ds_" + maDP).html("Khách đã trả phòng").hide().fadeIn();
                setTimeout(function () {
                    $("#notice_ds_" + maDP).fadeOut("slow", function () {
                        //$("#notice" + maLoai).remove();
                    });
                }, 2000);
                $("#checkOut_" + maDP).fadeOut(300).remove()
            }
        });
    });


});